package com.runa.rccprotocol.analysis;

import com.runa.monitor.comm.dto.newComm.HotMeterVO;
import com.runa.protocol.analysis.MetaBufferAnalysis;
import com.runa.protocol.util.ByteUtil;
import com.runa.rccprotocol.dto.equdata.HeatMeterData;
import com.runa.rccprotocol.dto.equdata.TerminalCalendarClock;
import com.runa.rccprotocol.dto.runa.RunaConcenResponseData;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class HZDeviceAnalysis extends MetaBufferAnalysis<byte[]> {

    private final SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmm");

    @Override
    public int fill(byte[] data, int length,String netEquNo) {
        byte func = data[0];
        switch (func) {
            case 2:
                break;
            case 6:
                reportData(data, 1);
                break;
        }
        return 0;
    }

    private void reportData(byte[] data, int offset) {
        int frames = data[offset++];
        int currFrame = data[offset++];
        int meters = data[offset++] & 0xff;
        Date readTime = null;
        try {
            readTime = sdf.parse(ByteUtil.byteToHexString(data, offset, 5));
        } catch (ParseException e) {
            
        }
        offset += 5;
        RunaConcenResponseData responseData = new RunaConcenResponseData();
        for (int i = 0; i < meters; i++) {
            int index = data[offset++] & 0xff;
            byte type = data[offset++];
            int bytes = data[offset++];
            switch (type) {
                case 5:
                    HotMeterVO meterData = new HotMeterVO();
                    meterData.setIndexNo(index - 64);
                    meterData.setReadDate(readTime);
                    if (data[offset++] == 0x26) {
                        String hex = ByteUtil.byteToHexString(data, offset, 57);
                        try {
                            Date date = sdf.parse(hex.substring(3, 13));
                            meterData.setMeterDate(date);
                        } catch (ParseException e) {
                            
                        }
                        float velocity = (float) (Integer.parseInt(hex.substring(13, 21)) * 0.001);
                        meterData.setFlow((float) (Integer.parseInt(hex.substring(21, 34)) * 0.0001));
                        meterData.setAccFlow((float) (Long.parseLong(hex.substring(34, 47)) * 0.001));
                        meterData.setWorkTime(Integer.parseInt(hex.substring(47, 54)));
                        meterData.setPower((float) (Integer.parseInt(hex.substring(77, 90), 16) * 0.0001));
                        meterData.setAccHeat((float) (Integer.parseInt(hex.substring(90, 103)) * 0.001));
                        meterData.setSupplyTemp((float) (Integer.parseInt(hex.substring(103, 108)) * 0.01));
                        meterData.setReturnTemp((float) (Integer.parseInt(hex.substring(108, 113)) * 0.01));
                    } else {
                        meterData.setSuccess(false);
                    }
                    offset += bytes - 1;
                    responseData.addRepData(meterData);
                    break;
                case 6:
                    break;
                case 7:
                    meterData = new HotMeterVO();
                    meterData.setIndexNo(index);
                    meterData.setReadDate(readTime);
                    if (data[offset++] == 0x26) {
                        float accCold = ByteUtil.bcdToInt(data, offset, 4, true);
//                        meterData.setSettDateHeatColdVal((float) (accCold * 0.1));
                        offset += 4;
                        float accHeat = ByteUtil.bcdToInt(data, offset, 4, true);
                        meterData.setAccHeat((float) (accHeat * 0.1));
                        offset += 4;
                        meterData.setFlow((float) (ByteUtil.bcdToInt(data, offset, 4, true) * 0.001));
                        offset += 4;
                        float accFlow = ByteUtil.bcdToInt(data, offset, 4, true);
                        meterData.setAccFlow((float) (accFlow * 0.1));
                        offset += 4;
                        meterData.setSupplyTemp((float) (ByteUtil.bcdToInt(data, offset, 4, true) * 0.01));
                        offset += 4;
                        meterData.setReturnTemp((float) (ByteUtil.bcdToInt(data, offset, 4, true) * 0.01));
                        offset += 4;
                        meterData.setPower((float) (ByteUtil.bcdToInt(data, offset, 4, true) * 0.1));
                        offset += 4;
                        meterData.setWorkTime(ByteUtil.bcdToInt(data, offset, 4, true));
                        offset += 4;
                        meterData.setFaultCode(meterFaults(data[offset] & 0xff));
                        offset += 2;
                    } else {
                        meterData.setSuccess(false);
                        offset += bytes;
                    }
                    responseData.addRepData(meterData);
                    break;
                case 9:
                    offset += bytes;
                    break;
            }
        }
        this.responseData = responseData;
    }

    private ArrayList<String> meterFaults(int fault) {
        ArrayList<String> list = new ArrayList<>();
//        StringBuilder fb = new StringBuilder();
        if (fault == 1 || fault == 2 || fault == 3 || fault == 4) {
            list.add("电池欠压");
//            fb.append("电池欠压").append(",");
        }
        if (fault == 0x10 || fault == 0x20) {
//            fb.append("进水故障").append(",");
            list.add("进水故障");
        }
        if (fault == 0x40 || fault == 0x80) {
//            fb.append("回水故障").append(",");
            list.add("回水故障");
        }
        return list;
    }

    public static void main(String[] args) {
        HuiZhong41Analysis huiZhong41Analysis = new HuiZhong41Analysis(new HZDeviceAnalysis());
        byte[] bytes = ByteUtil.hexStringToByte("403A001813706314299F012E1F46030000000011034009410542055B0D0A");
        huiZhong41Analysis.fill(bytes, bytes.length,"");
        huiZhong41Analysis.gain();
    }
}
